#ifndef _CALENDAR_H_
#define _CALENDAR_H_
#include <ctime>
#include <string>
#include <cstring>
#include <iostream>
using namespace std;

class Calendar
{
	struct tm _tm;
public:
	Calendar(void)
	{
		update();
	}
	Calendar(const string& fmt, const string& s)
	{
		fromString(fmt, s);
	}
	void update(void)
	{
		time_t t = time(0);
		_tm = *localtime(&t);
	}
	string toString(const string& fmt = "%F %T")
	{
		char buf[128] = {0};
		strftime(buf, sizeof(buf), fmt.empty() ? "%F %T" : fmt.c_str(), &_tm);
		return buf;
	}
	bool fromString(const string& fmt, const string& s)
	{
		if(s.empty() || fmt.empty())
		{
			return false;
		}
		memset(&_tm, 0, sizeof(_tm));
		if(strptime(s.c_str(), fmt.c_str(), &_tm) == false)
		{
			return false;
		}
		cout << toString() << endl;
		return true;
	}
	int year	(void)const{return _tm.tm_year+1900;}
	int month	(void)const{return _tm.tm_mon+1;}
	int day		(void)const{return _tm.tm_mday;}
	int hour	(void)const{return _tm.tm_hour;}
	int minute	(void)const{return _tm.tm_min;}
	int second	(void)const{return _tm.tm_sec;}
	int weekday	(void)const{return _tm.tm_wday;}
	int monthday(void)const{return _tm.tm_mday;}
	int yearday	(void)const{return _tm.tm_yday;}
};

#endif//_CALENDAR_H_
